package net;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author： pang-yun
 * @date： 2021-09-06 10:44
 * 功能目的 ：用于  将服务器注册进去
 */

public class SelectorServer {
    public static void main(String[] args) throws IOException {
        //- 注册中心  服务器   拿到注册码  确定只接受什么类型
        Selector selector = Selector.open();
        ServerSocketChannel ssc = ServerSocketChannel.open();
        ssc.configureBlocking(false);

        SelectionKey selectionKey = ssc.register(selector, 0, null);
        selectionKey.interestOps(SelectionKey.OP_ACCEPT);

        ssc.bind(new InetSocketAddress(9999));

        while (true){
            selector.select();

            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()){
                SelectionKey next = iterator.next();
                next.cancel();
            }
        }
    }
}
